package ldh.text.util;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class StringUtil {

    /**
     * 找到最后一个换行符
     * @param data
     * @param length
     * @return
     */
    public static int lastedLine(byte[] data, int length) {
        for (int i=length-1; i>0; i--) {
            if (data[i] == '\r' || data[i] == '\n') {
                return i+1;
            }
        }
        return length;
    }

    /**
     * 找到最后一个换行符
     * @param data
     * @param length
     * @return
     */
    public static int lastedLine(ByteBuffer data, int length) {
        for (int i=length-1; i>0; i--) {
            if (data.get(i) == '\r' || data.get(i) == '\n') {
                return i+1;
            }
        }
        return length;
    }

    /**
     * 找到最后一个换行符
     * @param data
     * @param length
     * @return
     */
    public static int lastedLine(char[] data, int length) {
        for (int i=length-1; i>0; i--) {
            if (data[i] == '\r' || data[i] == '\n') {
                return i+1;
            }
        }
        return length;
    }

    /**
     * 找到最后一个换行符
     * @param data
     * @param length
     * @return
     */
    public static int lastedLine(CharBuffer data, int length) {
        for (int i=length-1; i>0; i--) {
            if (data.get(i) == '\r' || data.get(i) == '\n') {
                return i+1;
            }
        }
        return length;
    }
}
